﻿using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc.Html;

namespace System.Web.Mvc.Ajax
{
    public static partial class AjaxExtensions
    {
        public static MvcHtmlString DialogLink(this AjaxHelper ajaxHelper, string linkText, string actionName, object routeDatas)
        {
            return ajaxHelper.ActionLink(linkText, actionName, routeDatas, new AjaxOptions
            {
                OnSuccess = "ajaxHelper.showDialog",
                HttpMethod = "GET"
            });
        }

        public static MvcHtmlString DropDownList(this AjaxHelper ajaxHelper, string name, string url, string updateId, IEnumerable<SelectListItem> selectList, string optionLabel = null, object htmlAttributes = null)
        {
            var htmlHelper = new HtmlHelper(ajaxHelper.ViewContext, ajaxHelper.ViewDataContainer, ajaxHelper.RouteCollection);
            var attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes ?? new { });
            attrs.Add("data-ajax", "true");
            attrs.Add("update-id", updateId);
            attrs.Add("url", url);
            return htmlHelper.DropDownList(name, selectList, optionLabel, attrs);
        }

        public static MvcHtmlString DropDownListFor<TModel, TProperty>(this AjaxHelper<TModel> ajaxHelper, Expression<Func<TModel, TProperty>> expression, string url, string updateId, IEnumerable<SelectListItem> selectList, string optionLabel = null, object htmlAttributes = null)
        {
            var htmlHelper = new HtmlHelper<TModel>(ajaxHelper.ViewContext, ajaxHelper.ViewDataContainer, ajaxHelper.RouteCollection);
            var attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes ?? new { });
            attrs.Add("data-ajax", "true");
            attrs.Add("update-id", updateId);
            attrs.Add("url", url);
            return htmlHelper.DropDownListFor(expression, selectList, optionLabel, attrs);
        }
    }
}